package com.sservice.commonBusiness.service.impl;

import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.e.PLetter;
import com.sservice.commonBusiness.mapper.PLetterMapper;
import com.sservice.commonBusiness.service.PLetterService;
import com.sservice.commonBusiness.vo.PLetterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Service
public class PLetterServiceImpl implements PLetterService {

    @Autowired
    private PLetterMapper pLetterMapper;

    @Override
    public List<PLetterVo> queryByPage(PLetterVo user) {
        Example example = new Example(PLetter.class);
        example.createCriteria()
                .andEqualTo("readStatus", user.getReadStatus())
                .andEqualTo("status", user.getStatus());
        return pLetterMapper.query(user);
    }

    @Override
    public int queryCount(PLetterVo user) {
        return pLetterMapper.count(user);
    }

    /**
     * 私信
     *
     * @param msg
     * @param from
     * @param to
     * @return
     */
    @Override
    public int toMsg(String msg, String from, String to, String remark) {
        PLetter pLetter = new PLetter();
        pLetter.setRemark(remark);
        pLetter.setFromUser(from);
        pLetter.setToUser(to);
        pLetter.setContent(msg);
        pLetter.setStatus(1);
        pLetter.setReadStatus(0); // 未读状态
        pLetter.setCreateAt(new Date());
        return pLetterMapper.insert(pLetter);
    }

    @Override
    public ListResult page(PLetterVo user) {
        List<PLetterVo> list = pLetterMapper.query(user);
        ListResult lr = new ListResult(true, list);
        Pagination pagination = new Pagination(user.getPage(), pLetterMapper.count(user), user.getSize());
        lr.setPage(pagination);
        return lr;
    }

    @Override
    public ListResult pagePlain(PLetterVo user) {
        List<PLetterVo> list = pLetterMapper.queryPlain(user);
        ListResult lr = new ListResult(true, list);
        Pagination pagination = new Pagination(user.getPage(), pLetterMapper.count(user), user.getSize());
        lr.setPage(pagination);
        return lr;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return pLetterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PLetter record) {
        return pLetterMapper.insert(record);
    }

    @Override
    public int insertSelective(PLetter record) {
        return pLetterMapper.insertSelective(record);
    }

    @Override
    public PLetter selectByPrimaryKey(Integer id) {
        return pLetterMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PLetter record) {
        return pLetterMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PLetter record) {
        return pLetterMapper.updateByPrimaryKey(record);
    }
}
